home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: array passing problem
- Date: Wed, 31 Jan 1996 13:21:30 GMT
- Organization: Netcom
- Message-ID: <310f6bb8.49956992@nntp.ix.netcom.com>
- References: <cl3kj4C00bkWMWlEJH@andrew.cmu.edu>
- NNTP-Posting-Host: ix-dc6-05.ix.netcom.com
- X-NETCOM-Date: Wed Jan 31 5:21:51 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Chie B Ng <cn0v+@andrew.cmu.edu> wrote:
-
- > Hello, sorry to bother you, but I can not see what the problem is with
- > this code fragment. When I use gcc -ansi to compile this I receive this
- > message: "warning: passing arg 1 of 'average' from incompatible pointer
- > type". When I run it, I get a segmentation fault in the average
- > procedure. Thanks.
- >
- >
- > /* headers go here.... */
- >
- > typedef unsigned char pixel;
- >
- > double average(pixel **a, int rows, int columns)
- > {
- > int i, j, sum = 0;
- >
- > /* since char is an int, there is no problem with + */
- >
- > for (i = 0 ; i < rows ; i++)
- > for (j = 0 ; j < columns ; j++)
- > sum += a[i][j];
- >
- > return ((double)sum)/(rows*columns);
- > }
- >
- >
- > void main(int argc, char **argv[])
- > {
- > double avg;
- > pixel image[200][100];
- >
- > /* initialize image with values */
- > /* .... */
- >
- > avg = average(image, 200, 100);
- >
- > printf("%f", avg);
- >
- > exit(0);
- > }
-
- In average, the first parameter is a pointer to pointer to pixel. The
- argument you are passing is an array of array of pixel. These are
- different and incompatible types.
-
- In most expressions an array is converted to a pointer, but that's
- all. In the call to average image is converted to a pointer to an
- array of 100 pixel (pixel (*)[100]). It is not converted to a pointer
- to pointer to pixel.
-
- Either change the definition of average to accept an array, e.g.,
-
- double average(pixel a[][100], int rows, int columns)
-
- or change main to define image as
-
- pixel **image;
-
- In the latter case you'll have to allocate memory for image.
-
-
- Michael M Rubenstein
-